package com.css.gdzj.gdzjbxgd.modules.exportWord;

import com.css.gdzj.gdzjbxgd.common.util.Constant;
import com.css.gdzj.gdzjbxgd.common.util.export.ExportWordUtils;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.BxgdReportInfoEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.BxgdReportInfoEntityVo;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestGiftChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestPtjobChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.service.BxgdReportInfoService;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.entity.UserInfoEntity;
import com.css.gdzj.gdzjbxgd.modules.userinfo.service.UserInfoService;
import com.css.gdzj.gdzjbxgd.sys.dict.entity.SysDictEntity;
import com.css.gdzj.gdzjbxgd.sys.dict.service.SysDictService;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import freemarker.template.Configuration;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * @author Mr.Jin
 * @create 2021-01-07 10:27
 */
@Controller
public class ExportController {

    @Autowired
    private BxgdReportInfoService bxgdReportInfoService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SysDictService sysDictService;
    @PostMapping("/reportExport/doc")
    @ResponseBody
    public void exportDoc(@RequestBody Map<String, String> params, HttpServletResponse resp) throws IOException {
        String userId = params.get("userId");
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(this.getClass(), "/templates");
        Template template = configuration.getTemplate("gdmb.ftl");
        BxgdReportInfoEntityVo bxgdReportInfo = bxgdReportInfoService.getBxgdReportInfo(userId);
        //大表
        BxgdReportInfoEntity bxgdReportbig = bxgdReportInfo.getBxgdReportInfoEntity();
        //小表 gift
        List<HonestGiftChildEntity> giftReport = bxgdReportInfo.getHonestGiftChildEntityList();
        //小表
        List<HonestPtjobChildEntity> ptjobReport = bxgdReportInfo.getHonestPtjobChildEntityList();
        UserInfoEntity userInfo = userInfoService.getUserInfo(userId);
        //获取填报时间
        String createTimeFirst = String.valueOf(bxgdReportbig.getCreateTime());
        String createTime = createTimeFirst.substring(0, createTimeFirst.indexOf(" "));
        String[] split = createTime.split("-");
        HashMap<String, String> timeMap = new HashMap<>();
        timeMap.put("year",split[0]);
        timeMap.put("month",split[1]);
        timeMap.put("day",split[2]);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("vo",bxgdReportbig);
        dataMap.put("gift",giftReport);
        dataMap.put("ptjob",ptjobReport);
        /*更换userinfo的字典项*/
        List<SysDictEntity> outlook= sysDictService.getDictNameOnly(Constant.OUT_LOOK,userInfo.getUserPoliticalOutlook());
        String outLookName = outlook.get(0).getDictName();
        List<SysDictEntity> unit = sysDictService.getDictNameOnly(Constant.UNIT, userInfo.getUserUnit());
        String unitName = unit.get(0).getDictName();
        userInfo.setUserPoliticalOutlook(outLookName);
        userInfo.setUserUnit(unitName);
        dataMap.put("userInfo",userInfo);
        dataMap.put("time",timeMap);
        FileInputStream fin=null;
        ServletOutputStream outputStream=null;
        try {
            File wordfile = ExportWordUtils.createDoc(dataMap, template);
            fin = new FileInputStream(wordfile);
            resp.setCharacterEncoding("UTF-8");
            resp.setContentType("application/msword");
            resp.setHeader("Content-Disposition", "attachment;filename=" + new String("UserInfoTest.doc".getBytes("utf-8"), "ISO8859-1"));
            outputStream = resp.getOutputStream();
            byte[] buffer = new byte[1024];
            int bytesToRead = -1;
            while ((bytesToRead = fin.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesToRead);
            }
        }catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (fin != null)
                fin.close();
            if (outputStream != null)
                outputStream.close();
        }
    }
}
